package io.github.annyylxyms.gyncache.command;

import io.github.annyylxyms.gyncache.core.Command;
import io.github.annyylxyms.gyncache.core.GynCache;
import io.github.annyylxyms.gyncache.core.Reply;

/**
 *
 * @author Anny
 * @date 2025/1/15 17:31
 */
public class RpopCommand implements Command {
    @Override
    public String name() {
        return "RPOP";
    }

    @Override
    public Reply<?> exec(GynCache cache, String[] args) {
        String key = getKey(args);
        int count = 1;
        if (args.length > 6) {
            String var = getVal(args);
            count = Integer.parseInt(var);
            return Reply.array(cache.rpop(key, count));
        }

        String[] rpop = cache.lpop(key, count);
        return Reply.bulkString(rpop == null ? null : rpop[0]);
    }
}
